<?php
/**
 * Created by PhpStorm.
 * User: Sky
 * Date: 2017/8/3
 * Time: 下午3:12
 */

namespace app\models;


use app\librarys\DbCacheUtility;
use app\librarys\Tools;

class Message
{
    public static function getSystemMessageCount($title)
    {
        $cmd = \Yii::$app->db->createCommand('select count(1) from t_system_message where title like :title');
        $cmd->bindValue(':title', '%' . $title . '%');
        $count = DbCacheUtility::queryScalar($cmd);
        return $count;
    }

    public static function getSystemMessageList($page, $pageSize = 20, $title)
    {
        $cmd = \Yii::$app->db->createCommand('select * from t_system_message where title like :title ORDER BY create_datetime DESC limit :offset , :limit');
        $cmd->bindValue(':title', '%' . $title . '%');
        $cmd->bindValue(':offset', $page * $pageSize);
        $cmd->bindValue(':limit', $pageSize);
        $items = DbCacheUtility::queryAll($cmd);
        return $items;
    }

    /**
     * 根据id获取系统消息
     * @param $id
     * @return mixed
     */
    public static function getById($id)
    {
        $cmd = \Yii::$app->db->createCommand('SELECT * FROM t_system_message WHERE id = :id');
        $cmd->bindValue(':id', $id);
        $item = DbCacheUtility::queryOne($cmd);
        return $item;
    }

    /**
     * save系统消息
     * @param $id
     * @param $title
     * @param $content
     * @return bool
     */
    public static function save($id, $title, $content)
    {
        $cmd = \Yii::$app->db->createCommand('
            INSERT INTO t_system_message(`id`, `title`, `content`)
            VALUES(:id, :title, :content)
            ON DUPLICATE KEY
            UPDATE `title`=:title, `content`=:content;
        ');
        $cmd->bindValue(':id', $id == '0' ? Tools::db_uuid() : $id);
        $cmd->bindValue(':title', $title);
        $cmd->bindValue(':content', $content);
        return DbCacheUtility::execute($cmd) > 0;
    }

    public static function getFeedbackCount()
    {
        $cmd = \Yii::$app->db->createCommand('select count(1) from t_feedback');
        $count = DbCacheUtility::queryScalar($cmd);
        return $count;
    }

    public static function getFeedbackList($page, $pageSize = 20)
    {
        $cmd = \Yii::$app->db->createCommand('
            select 
            t0.*, 
            CASE WHEN origin=1 THEN t1.`name` WHEN origin=2 THEN t2.`name` END as origin_name
            from t_feedback t0
            LEFT join t_merchant t1 ON t0.origin=1 AND t1.id=t0.origin_id
            LEFT join t_user t2 on t0.origin=2 and t2.id=t0.origin_id
            order by t0.create_datetime desc 
            LIMIT :offset , :limit');
        $cmd->bindValue(':offset', $page * $pageSize);
        $cmd->bindValue(':limit', $pageSize);
        $items = DbCacheUtility::queryAll($cmd);
        return $items;
    }

    public static function getFeedbackById($id)
    {
        $cmd = \Yii::$app->db->createCommand('
            select 
            t0.*, 
            CASE WHEN origin=1 THEN t1.`name` WHEN origin=2 THEN t2.`name` END as origin_name
            from t_feedback t0
            LEFT join t_merchant t1 ON t0.origin=1 AND t1.id=t0.origin_id
            LEFT join t_user t2 on t0.origin=2 and t2.id=t0.origin_id
            where t0.id=:id;');
        $cmd->bindValue(':id', $id);
        $item = DbCacheUtility::queryOne($cmd);
        return $item;
    }

    public static function setFeedbackRead($id)
    {
        $cmd = \Yii::$app->db->createCommand('update t_feedback set has_read=1 where id=:id;');
        $cmd->bindValue(':id', $id);
        DbCacheUtility::execute($cmd);
    }
}